package com.wonder.app.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.PrdIronTray;
import com.wonder.app.domain.PrdPick;
import com.wonder.app.request.PrdIronTrayRequest;
import com.wonder.app.request.PrdPickRequest;
import com.wonder.app.service.PrdIronTrayService;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.framework.authorization.CheckAdminToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "铁托")
@RestController
@RequestMapping("/app/iron")
public class PrdIronTrayController {

    @Autowired
    private PrdIronTrayService prdIronTrayService;

    @Autowired
    private CheckAdminToken checkAdminToken;

    @Autowired
    private SysAdminService adminService;


    @ApiOperation(value = "生产领料列表")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String,Object> index(@RequestBody PrdIronTrayRequest prdIronTrayRequest) {
        Page<PrdIronTray> page = new Page<PrdIronTray>(prdIronTrayRequest.getPage(), prdIronTrayRequest.getLimit());
        IPage<PrdIronTray> list = prdIronTrayService.selectList(prdIronTrayRequest, page);

        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data", CommonPage.restPage(list));


        return AjaxMap.success(retMap);
    }


    /**
     * 修改托盘数量(pda)
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "校验铁托是否存在")
    @ResponseBody
    @PostMapping(value = "/verify_iron_tray")
    public AjaxResult<?> verifyIronTray (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        boolean b = prdIronTrayService.verifyIronTray(map);
        return AjaxResult.success(b);
    }


    /**
     * 释放铁托占用(pda)
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "校验铁托是否存在")
    @ResponseBody
    @PostMapping(value = "/release_iron")
    public AjaxResult<?> releaseIron (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        boolean b = prdIronTrayService.releaseIron(userName,map);
        return AjaxResult.success(b);
    }

    /**
     * 增加铁托
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "增加铁托")
    @ResponseBody
    @PostMapping(value = "/add")
    public AjaxResult<?> add (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        boolean b = prdIronTrayService.addIronTray(userName,map);
        return AjaxResult.success(b);
    }

}
